package com.ywb.ex01;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 * @author ywb
 */
public class Task01 extends JFrame {


    private JPanel contentPane;
    private JTextField textField;
    private JTable table;
    private File directory;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Task01 frame = new Task01();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Task01() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel label = new JLabel("输入文件扩展名：");
        label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        panel.add(label);

        textField = new JTextField();
        textField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        panel.add(textField);
        textField.setColumns(12);

        JButton button = new JButton("选择文件夹");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        panel.add(button);

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        table = new JTable();
        JTableHeader header = table.getTableHeader();
        header.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        header.setPreferredSize(new Dimension(header.getWidth(), 25));// 修改表头的高度
        table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        table.setRowHeight(25);
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setColumnIdentifiers(new String[] { "文件名", "文件大小", "修改时间" });
        scrollPane.setViewportView(table);

    }
    protected void do_button_actionPerformed(ActionEvent e){
//        从C盘打开
        JFileChooser chooser = new JFileChooser("D:");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            directory = chooser.getSelectedFile();
        }
        if(directory == null){
            return;
        }
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-DD");
        File[] files = directory.listFiles();
        for(File file : files){
            if(file.getName().endsWith(textField.getText())){
                model.addRow(new String[]{file.getName(), formatFileSize(file), df.format(file.lastModified())});
            }
        }
        if(model.getColumnCount() == 0){
            model.addRow(new String[]{"当前文件夹暂无当前后缀文件"});
        }
        table.setModel(model);
    }

    /**
     * 返回file格式化后的长度
     * @param f
     * @return
     */
    private String formatFileSize(File f) {
        long fileS = f.length();
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        return fileSizeString;
    }

}
